//
//  LJOneAddressPicker.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2021/6/23.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "LJOneAddressPicker.h"

@interface LJOneAddressPicker () <UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *pickView;
@property (nonatomic, strong) UIView *baseView;  //设置一下背景View
@property (nonatomic, strong) LJOneAddressModel *selectedAddress;

@end

@implementation LJOneAddressPicker

-(LJOneAddressModel *)selectedAddress {
    if (!_selectedAddress) {
        _selectedAddress = [LJOneAddressModel new];
    }
    return _selectedAddress;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = [[UIScreen mainScreen] bounds];
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self setBaseView];
    }
    return self;
}

-(void)setDataArray:(NSArray<LJOneAddressModel *> *)dataArray {
    _dataArray = dataArray;
    [self.pickView reloadAllComponents];
    [self.pickView selectRow:0 inComponent:0 animated:YES];
    self.selectedAddress = [self.dataArray objectAtIndex:[self.pickView selectedRowInComponent:0]];
}

-(void)setCurrentModel:(LJOneAddressModel *)currentModel {
    _currentModel = currentModel;
    [self updateAddressWithCurrentAddress];
}

- (void)setBaseView {
    self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH, ScreenW, 250 + BottomHeight)];
    self.baseView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.baseView];
    
    self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 205)];
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    [self.baseView addSubview:self.pickView];
    
    //设置取消按钮
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame  = CGRectMake(0, 205, ScreenW/2, 45);
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:HexColor(0x999999) forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:cancleBtn];

    //设置确定按钮
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(ScreenW/2, 205, ScreenW/2, 45);
    [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [sureBtn setTitleColor:HexColor(0x999999) forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:sureBtn];
    [self.pickView reloadAllComponents];
}

- (void)updateAddressWithCurrentAddress {
    if (self.currentModel && self.dataArray.count) {
        [self.dataArray enumerateObjectsUsingBlock:^(LJOneAddressModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.Id == self.currentModel.Id) {
//                self.selectedAddress = self.currentModel;
                [self.pickView selectRow:idx inComponent:0 animated:YES];
            }
        }];
    }
}

- (void)pickerViewShow {
    [MAIN_WINDOW addSubview:self];
    [UIView animateWithDuration:0.2 animations:^{
        self.baseView.nim_top =ScreenH-250-BottomHeight;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            self.baseView.nim_top = ScreenH-250-BottomHeight;
        } completion:^(BOOL finished) {
            
        }];
    }];
}

//点击完成按钮的传值
-(void)finishClick{
    if (self.finishBlock) {
        self.finishBlock(self.selectedAddress);
    }
    [self hide];
}

//取消按钮的点击事件
-(void)hide{
//    if (self.finishBlock) {
//        self.finishBlock(nil);
//    }
    [UIView animateWithDuration:0.2 animations:^{
        self.baseView.nim_top = ScreenH;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hide];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:14]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.dataArray objectAtIndex:row].name;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return self.frame.size.width;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.selectedAddress = self.dataArray[row];
}

@end

@implementation LJOneAddressModel

+(NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"Id":@"id"};
}

+ (NSDictionary *)mj_objectClassInArray {
    return @{@"children":@"LJOneAddressModel"};
}

@end
